package com.zcs.app.lucky.adapter

import androidx.recyclerview.widget.RecyclerView
import cn.sxw.android.base.adapter.CommonRecyclerAdapter
import cn.sxw.android.base.utils.GlideStaticUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zcs.app.lucky.R
import com.zcs.app.lucky.entity.LuckyResultEntity
import java.util.*

class ResultAdapterHelper {
    // 奖品列表
    var prizeAdapter: CommonRecyclerAdapter<LuckyResultEntity>? = null
    val mItems = ArrayList<LuckyResultEntity>()

    fun initAdapter(
        recyclerView: RecyclerView,
        onClick: (item: LuckyResultEntity) -> Unit
    ) {
        if (prizeAdapter == null) {
            prizeAdapter =
                object : CommonRecyclerAdapter<LuckyResultEntity>(
                    R.layout.item_lucky_result_row,
                    mItems
                ) {
                    override fun convert(holder: BaseViewHolder, item: LuckyResultEntity) {
                        val prize = item.prize!!

                        holder.setText(R.id.tvName, prize.name)
                        holder.setText(R.id.tvLevel, prize.level!!.name)
                        GlideStaticUtils.displayImage(
                            prize.pic,
                            R.drawable.empty_photo,
                            holder.getView(R.id.ivThumb)
                        )
                        val adapterHelper = AdapterHelper()
                        adapterHelper.userList.clear()
                        adapterHelper.userList.addAll(item.userList)
                        val rvUsers = holder.getView<RecyclerView>(R.id.rvUsers)
                        adapterHelper.initUserAdapter(rvUsers, spanCount = 2) {}
                    }
                }
            recyclerView.adapter = prizeAdapter
            prizeAdapter!!.setOnItemClickListener { adapter, _, position ->
                val clickItem = mItems[position]
                onClick(clickItem)
            }
        } else {
            prizeAdapter!!.notifyDataSetChanged()
        }
    }
}